package zheezes.protocol.impl.gd.bean;

import java.math.BigDecimal;

import zheezes.protocol.impl.gd.converter.MeterValueConverter;
import zheezes.util.ArrayUtil;


public class VipMeterData {
	private byte[] mtno = new byte[6];
	private BigDecimal[] values = new BigDecimal[24];

	public int setBytes(byte[] buf, int offset, int max) {
		int idx = offset;
		System.arraycopy(buf, idx, mtno, 0, 6);
		ArrayUtil.reverse(mtno);
		idx += 6;
		byte[] tv = new byte[4];
		for (int i = 0; i < 24; i++) {
			try{
			System.arraycopy(buf, idx, tv, 0, 4);
			ArrayUtil.reverse(tv);
			values[i] = MeterValueConverter.btov(tv, 0);
			idx += 4;
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
		assert idx >= max;
		return idx - offset;
	}

	public byte[] getBytes() {
		byte[] buf = new byte[size()];
		getBytes(buf, 0);
		return buf;
	}
	
	public int getBytes(byte[] buf, int offset) {
		int idx = offset;
		System.arraycopy(mtno, 0, buf, idx, 6);
		ArrayUtil.reverse(buf, idx, 6);
		idx += 6;
		for (int i = 0; i < 24; i++) {
			MeterValueConverter.vtob(buf, idx, values[i]);
			ArrayUtil.reverse(buf, idx, 4);
			idx += 4;
		}
		return idx - offset;
	}
	
	public int size() {
		return 102;
	}

	public byte[] getMtno() {
		return mtno;
	}

	public void setMtno(byte[] mtno) {
		this.mtno = mtno;
	}

	public BigDecimal getValue(int hh) {
		return values[hh];
	}

	public void setValue(int hh, BigDecimal value) {
		values[hh] = value;
	}
}
